package com.dms.modules.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dms.modules.user.entity.Region;
import com.dms.modules.user.mapper.RegionMapper;
import com.dms.modules.user.service.RegionService;
import com.dms.modules.user.vo.RegionTreeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Override
    @Cacheable(value = "region:provinces")
    public List<Region> getProvinces() {
        return lambdaQuery()
                .eq(Region::getLevel, 1)
                .orderByAsc(Region::getCode)
                .list();
    }

    @Override
    @Cacheable(value = "region:cities", key = "#provinceCode")
    public List<Region> getCities(String provinceCode) {
        return lambdaQuery()
                .eq(Region::getParentCode, provinceCode)
                .eq(Region::getLevel, 2)
                .orderByAsc(Region::getCode)
                .list();
    }

    @Override
    @Cacheable(value = "region:districts", key = "#cityCode")
    public List<Region> getDistricts(String cityCode) {
        return lambdaQuery()
                .eq(Region::getParentCode, cityCode)
                .eq(Region::getLevel, 3)
                .orderByAsc(Region::getCode)
                .list();
    }

    @Override
    @Cacheable(value = "region:code", key = "#code")
    public Region getByCode(String code) {
        return lambdaQuery()
                .eq(Region::getCode, code)
                .one();
    }

    @Override
    @Cacheable(value = "region:tree")
    public List<RegionTreeVO> getRegionTree() {
        // 获取所有地区数据
        List<Region> allRegions = lambdaQuery()
                .orderByAsc(Region::getLevel)
                .orderByAsc(Region::getCode)
                .list();

        // 按父编码分组
        Map<String, List<Region>> parentCodeMap = allRegions.stream()
                .collect(Collectors.groupingBy(region -> region.getParentCode() == null ? "" : region.getParentCode()));

        // 构建树形结构，从省份开始
        return buildTree(parentCodeMap, "");
    }

    private List<RegionTreeVO> buildTree(Map<String, List<Region>> parentCodeMap, String parentCode) {
        List<RegionTreeVO> tree = new ArrayList<>();
        List<Region> regions = parentCodeMap.get(parentCode);
        
        if (regions != null) {
            for (Region region : regions) {
                RegionTreeVO node = new RegionTreeVO();
                BeanUtils.copyProperties(region, node);
                
                // 递归构建子节点
                List<RegionTreeVO> children = buildTree(parentCodeMap, region.getCode());
                if (!children.isEmpty()) {
                    node.setChildren(children);
                }
                
                tree.add(node);
            }
        }
        
        return tree;
    }
} 